package com.alibaba.citrus.ots.unified.inventory.repository.impl;

import com.alibaba.citrus.ots.unified.inventory.repository.AllocateApplyOrderDetailReadRepository;
import com.alibaba.citrus.ots.unified.inventory.utils.InfoUtil;
import com.alibaba.citrus.ots.unified.inventory.utils.OtsDateUtils;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorderdetail.dto.AllocateApplyOrderDetailLoadListRequest;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorderdetail.dto.LoadAllocateApplyOrderDetailRequest;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorderdetail.model.AllocateApplyOrderDetail;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorderdetail.service.AllocateApplyOrderDetailService;
import com.epoch.app.otsunifiedinventory.dto.response.AllocateApplyOrderGoodsDetailDTO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author heyu
 * @description
 */
@Component
public class AllocateApplyOrderDetailReadRepositoryImpl implements AllocateApplyOrderDetailReadRepository {

    @Resource
    private AllocateApplyOrderDetailService allocateApplyOrderDetailService;

    @Override
    public List<AllocateApplyOrderGoodsDetailDTO> loadDetailByOrderId(Long id) {
        AllocateApplyOrderDetailLoadListRequest allocateApplyOrderDetailLoadListRequest = new AllocateApplyOrderDetailLoadListRequest();
        if (Objects.isNull(id)){
            return null;
        }
        allocateApplyOrderDetailLoadListRequest.setAllocateApplyOrderId(id.intValue());
        allocateApplyOrderDetailLoadListRequest.setLimit(1000);
        Result<List<AllocateApplyOrderDetail>> result = allocateApplyOrderDetailService.loadAllocateApplyOrderDetailList(allocateApplyOrderDetailLoadListRequest);
        if (CollectionUtils.isNotEmpty(result.getResult())) {
            List<AllocateApplyOrderDetail> list = result.getResult();
            return list.stream().map(allocateApplyOrderDetail -> {
                AllocateApplyOrderGoodsDetailDTO allocateApplyOrderGoodsDetailDTO = new AllocateApplyOrderGoodsDetailDTO();
                BeanUtils.copyProperties(allocateApplyOrderDetail, allocateApplyOrderGoodsDetailDTO);
                allocateApplyOrderGoodsDetailDTO.setTitle(allocateApplyOrderDetail.getGoodsName());
                allocateApplyOrderGoodsDetailDTO.setScItemId(allocateApplyOrderDetail.getGoodsCode());
                allocateApplyOrderGoodsDetailDTO.setReturnGood(allocateApplyOrderDetail.getSendBackRequired());
                allocateApplyOrderGoodsDetailDTO.setProductionTime(getStringDate(allocateApplyOrderDetail.getProductionTime()));
                allocateApplyOrderGoodsDetailDTO.setExchangeAmt(allocateApplyOrderDetail.getExchangeAmt());
                if (StringUtils.isNotBlank(allocateApplyOrderGoodsDetailDTO.getUom())){
                    allocateApplyOrderGoodsDetailDTO.setUomName(InfoUtil.convertUnit(allocateApplyOrderGoodsDetailDTO.getUom()));
                }
                return allocateApplyOrderGoodsDetailDTO;
            }).collect(Collectors.toList());
        }
        return null;
    }
    private static String getStringDate(Date date){
        String s = "";
        if (Objects.nonNull(date)) {
            s = OtsDateUtils.formatDate(date, OtsDateUtils.DATE_FORMAT);
        }
        return s;
    }
    @Override
    public List<AllocateApplyOrderGoodsDetailDTO> loadDetailByOrderDetailIds(List<Long> ids) {
        List<AllocateApplyOrderGoodsDetailDTO> allocateApplyOrderGoodsDetailDTOS = new ArrayList<>();
        for (Long id:ids){
            LoadAllocateApplyOrderDetailRequest loadAllocateApplyOrderDetailRequest = new LoadAllocateApplyOrderDetailRequest();
            AllocateApplyOrderGoodsDetailDTO allocateApplyOrderGoodsDetailDTO = new AllocateApplyOrderGoodsDetailDTO();
            loadAllocateApplyOrderDetailRequest.setId(id.toString());
            AllocateApplyOrderDetail allocateApplyOrderDetail = allocateApplyOrderDetailService.loadAllocateApplyOrderDetail(loadAllocateApplyOrderDetailRequest);
            BeanUtils.copyProperties(allocateApplyOrderDetail,allocateApplyOrderGoodsDetailDTO);
            allocateApplyOrderGoodsDetailDTO.setTitle(allocateApplyOrderDetail.getGoodsName());
            allocateApplyOrderGoodsDetailDTO.setScItemId(allocateApplyOrderDetail.getGoodsCode());
            allocateApplyOrderGoodsDetailDTOS.add(allocateApplyOrderGoodsDetailDTO);
        }
        return allocateApplyOrderGoodsDetailDTOS;
    }

    @Override
    public List<AllocateApplyOrderDetail> queryDetailByOrderId(Long id) {
        AllocateApplyOrderDetailLoadListRequest allocateApplyOrderDetailLoadListRequest = new AllocateApplyOrderDetailLoadListRequest();
        allocateApplyOrderDetailLoadListRequest.setAllocateApplyOrderId(id.intValue());
        allocateApplyOrderDetailLoadListRequest.setLimit(1000);
        Result<List<AllocateApplyOrderDetail>> result = allocateApplyOrderDetailService.loadAllocateApplyOrderDetailList(allocateApplyOrderDetailLoadListRequest);
        if (Objects.nonNull(result)){
            return result.getResult();
        }
        return null;
    }

//    @Override
//    public PageResult<List<AllocateApplyOrderListDTO>> queryListDetails(AllocateApplyOrderQueryRequest allocateApplyOrderQueryRequest) {
//
//        AllocateApplyOrderDetailLoadListRequest allocateApplyOrderDetailLoadListRequest = new AllocateApplyOrderDetailLoadListRequest();
//        //预报单id
////        allocateApplyOrderDetailLoadListRequest.setAllocateApplyOrderId();
//        //创建时间
//        if (!Objects.isNull(allocateApplyOrderQueryRequest.getGmtCreate())) {
//            allocateApplyOrderDetailLoadListRequest.setGmtCreate(allocateApplyOrderQueryRequest.getGmtCreate());
//        }
//        //状态
//        if (!Objects.isNull(allocateApplyOrderQueryRequest.getStatus())) {
//            allocateApplyOrderDetailLoadListRequest.setStatus(allocateApplyOrderQueryRequest.getStatus());
//        }
//        //TODO 待我回复状态字段没加 先筛选状态
//        if (!Objects.isNull(allocateApplyOrderQueryRequest.getAgency())) {
////            allocateApplyOrderDetailLoadListRequest.setStatus(AllocateStatusEnum.WAIT_ANSWER.getCode());
//            allocateApplyOrderDetailLoadListRequest.setStatus(null);
//        }
//        allocateApplyOrderDetailLoadListRequest.setStart(allocateApplyOrderQueryRequest.getStart());
//        allocateApplyOrderDetailLoadListRequest.setLimit(allocateApplyOrderQueryRequest.getLimit());
//        allocateApplyOrderDetailLoadListRequest.setCreator(allocateApplyOrderQueryRequest.getCreator());
//        allocateApplyOrderDetailLoadListRequest.setDir("gmt_create");
//        allocateApplyOrderDetailLoadListRequest.setAsc(false);
//        Result<List<AllocateApplyOrderDetail>> result = allocateApplyOrderDetailService.loadAllocateApplyOrderDetailList(allocateApplyOrderDetailLoadListRequest);
//        if (CollectionUtils.isNotEmpty(result.getResult())) {
//            List<AllocateApplyOrderDetail> list = result.getResult();
//            List<AllocateApplyOrderListDTO> allocateApplyOrderGoodsDetailDTOs = list.stream().map(allocateApplyOrderDetail -> {
//                AllocateApplyOrderListDTO allocateApplyOrderListDTO = new AllocateApplyOrderListDTO();
//                allocateApplyOrderListDTO.setId(allocateApplyOrderDetail.getAllocateApplyOrderId() != null ? Long.valueOf(allocateApplyOrderDetail.getAllocateApplyOrderId()) : null);
//                allocateApplyOrderListDTO.setLineNo(allocateApplyOrderDetail.getLineNo());
//                allocateApplyOrderListDTO.setOutLineNo(allocateApplyOrderDetail.getOutLineNo());
//                allocateApplyOrderListDTO.setTitle(allocateApplyOrderDetail.getGoodsName());
//                allocateApplyOrderListDTO.setScItemId(allocateApplyOrderDetail.getGoodsCode());
//                Integer applyQuantity = allocateApplyOrderDetail.getApplyQuantity() != null ? allocateApplyOrderDetail.getApplyQuantity() : 0;
//                allocateApplyOrderListDTO.setApplyQuantity(applyQuantity);
//                Integer replyQuantity = allocateApplyOrderDetail.getReplyQuantity() != null ? allocateApplyOrderDetail.getReplyQuantity() : 0;
//                allocateApplyOrderListDTO.setReplyQuantity(replyQuantity);
//                if (Objects.nonNull(allocateApplyOrderDetail.getStatus())){
//                    allocateApplyOrderListDTO.setLineStatusName(EnumUtils.getAllocateStatusEnum(allocateApplyOrderDetail.getStatus()));
//                }
//                allocateApplyOrderListDTO.setNoReplyQuantity(applyQuantity - replyQuantity);
//                allocateApplyOrderListDTO.setDeliverQuantity(allocateApplyOrderDetail.getDeliverQuantity());
//                allocateApplyOrderListDTO.setUnitPrice(allocateApplyOrderDetail.getUnitPrice());
//                allocateApplyOrderListDTO.setDiscount(allocateApplyOrderDetail.getDiscount());
//                //TODO 小计金额 这里需要计算，数量×单价-折扣，现在数量未确认
//                return allocateApplyOrderListDTO;
//            }).collect(Collectors.toList());
//            return PageResult.of(allocateApplyOrderGoodsDetailDTOs, result.getTotal());
//        }
//        return null;
//    }
}
